package com.soc.web.client;

import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.soc.web.client.api.FancyBoxException;
import com.soc.web.client.api.FancyboxUtils;

public class Inline extends Media {
	
	private Panel addedPanel;
	private DivElement wrappingDiv;
	private DivElement innerDiv;
	private String linkId;
	
	public Inline(Panel inlinePanel) {
		this.addedPanel = inlinePanel;
		// Create the outer div
		wrappingDiv = Document.get().createDivElement();
		wrappingDiv.setAttribute("style", "display:none;");
		// Create the inner div
		innerDiv = Document.get().createDivElement();		
		innerDiv.appendChild(this.addedPanel.getElement());
		wrappingDiv.appendChild(innerDiv);
		
		// Get the id for the inner div
		linkId = FancyboxUtils.getUniqueId();
		
		// set the inner panels id 
		this.addedPanel.getElement().setId(linkId);
		this.setHref("#" + linkId);
	}
	
	@Override
	protected void onLoad() {
		try {
			load();
			// create a div on the DOM and then bind the panel during the bind phase.
			// add it to the main dom, doesn't need to be anywhere since it will be hidden.
			RootLayoutPanel.get().getElement().appendChild(wrappingDiv);
		} catch (FancyBoxException e) {
			e.printStackTrace();
		}
		super.onLoad();
	}

	public Panel getBindedPanel() {
		return addedPanel;
	}
	
	public String getLinkId() {
		return linkId;
	}
}
